package com.geeksaga.dao.ibatis.mysql;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.geeksaga.common.util.DateConvertor;
import com.geeksaga.common.util.DateUtil;
import com.geeksaga.dao.BbsTypeDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.BbsType;
import com.geeksaga.domain.TableRowCount;

/**
 * @author newbie, casper0402
 * @since 0.1
 */
@Repository
public class BbsTypeDaoImpl extends BaseIBatisDao implements BbsTypeDao {

	/* (non-Javadoc)
     * @see com.geeksaga.dao.BbsTypeDao#list()
     */
    @SuppressWarnings("unchecked")
	public List<BbsType> list() throws DataAccessException {
		return (List<BbsType>) getSqlMapClientTemplate().queryForList("BBS_TYPE.list");
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#list(java.util.Map)
	 */
	@SuppressWarnings("unchecked")
	public List<BbsType> list(Map<String, Object> parameter) throws DataAccessException {
		return (List<BbsType>) getSqlMapClientTemplate().queryForList("BBS_TYPE.listByParameter", parameter);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#count(java.util.Map)
	 */
	public TableRowCount count(Map<String, Object> parameter)
			throws DataAccessException {
		return (TableRowCount) getSqlMapClientTemplate().queryForObject("BBS_TYPE.count", parameter);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#add(com.geeksaga.domain.BbsType)
	 */
	public BbsType add(BbsType bbsType) throws DataAccessException {
		bbsType.setRegistTimestamp(DateConvertor.getDateTimeFormat());
		
		return (BbsType) getSqlMapClientTemplate().insert("BBS_TYPE.add", bbsType);
	}

    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsTypeDao#updateCnt(com.geeksaga.domain.BbsType, com.geeksaga.domain.BbsType.CNT_TYPE)
     */
    public int updateCnt(BbsType bbsType, BbsType.CNT_TYPE type) throws DataAccessException {
        Map<String, String> param = new HashMap<String, String>();
        param.put("bbsId", bbsType.getBbsId());
        
        if(!DateUtil.getBasicDate().equals(bbsType.getTodayDate())) {
            param.put("todayDate", DateUtil.getBasicDate());
            
            if (BbsType.CNT_TYPE.TODAY_PLUS_CNT == type)
                param.put("newTodayPlusCnt", "0");
            else if(BbsType.CNT_TYPE.TODAY_MINUS_CNT == type)
                param.put("newTodayMinusCnt", "0");
        } else {
            if (BbsType.CNT_TYPE.TODAY_PLUS_CNT == type) {
                param.put("todayPlusCnt", "0");
                param.put("totalPlusCnt", "0");
                param.put("recodePlusCnt", "0");
            } else if (BbsType.CNT_TYPE.TODAY_MINUS_CNT == type) {
                param.put("todayMinusCnt", "0");
                param.put("totalMinusCnt", "0");
            }
        }
        
        if (BbsType.CNT_TYPE.RECODE_MINUS_CNT == type)
            param.put("recodeMinusCnt", "0");

        return getSqlMapClientTemplate().update("BBS_TYPE.updateCnt", param);
    }

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#bbsUseUpdate(com.geeksaga.domain.BbsType)
	 */
	public int bbsUseModify(BbsType bbsType) throws DataAccessException {
		bbsType.setModifyTimestamp(DateConvertor.getDateTimeFormat());
		
		return getSqlMapClientTemplate().update("BBS_TYPE.bbsUseModify", bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#bbsView(com.geeksaga.domain.BbsType)
	 */
	public BbsType bbsView(BbsType bbsType) throws DataAccessException {
		return (BbsType) getSqlMapClientTemplate().queryForObject("BBS_TYPE.bbsTypeView", bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#bbsAuthorityModify(com.geeksaga.domain.BbsType)
	 */
	public int bbsAuthorityModify(BbsType bbsType) throws DataAccessException {
		bbsType.setModifyTimestamp(DateConvertor.getDateTimeFormat());
		
		return getSqlMapClientTemplate().update("BBS_TYPE.bbsAuthoriyModify", bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#bbsScreenSetUp(com.geeksaga.domain.BbsType)
	 */
	public int bbsScreenSetUp(BbsType bbsType) throws DataAccessException {

		return getSqlMapClientTemplate().update("BBS_TYPE.bbsScreenSetUp", bbsType);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsTypeDao#bbsSecuritySetUp(com.geeksaga.domain.BbsType)
	 */
	public int bbsSecuritySetUp(BbsType bbsType) throws DataAccessException {
		
		return getSqlMapClientTemplate().update("BBS_TYPE.bbsSecuritySetUp", bbsType);
	}
}